package com.zimmur.service.impl.goods;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.base.Throwables;
import com.zimmur.dao.goods.IGoodsSizeDao;
import com.zimmur.model.goods.Goods;
import com.zimmur.model.goods.GoodsColor;
import com.zimmur.model.goods.GoodsSize;
import com.zimmur.model.order.OrderDetail;
import com.zimmur.service.goods.IGoodsService;
import com.zimmur.service.goods.IGoodsSizeService;
import com.zimmur.service.order.IOrderDetailService;

@Service("goodsSizeService")
public class GoodsSizeServiceImpl implements IGoodsSizeService {
	private static Logger log = LoggerFactory.getLogger(GoodsSizeServiceImpl.class);
	
	@Resource
	private IGoodsSizeDao goodsSizeDao;
	@Resource
	private IGoodsService goodsService;
	@Resource
	private IOrderDetailService orderDetailService;
	
	@Override
	public List<GoodsSize> selectByGoodsId(long goodsId) {
		return goodsSizeDao.selectListByGoodsId(goodsId);
	}

	@Override
	public GoodsSize selectGoodsByGoodsIdAndColorAndSize(long goodsId, String colorCode, String size) {
		Map<String, Object> map = new HashMap<String,Object>();
		map.put("goodsId", goodsId);
		map.put("colorCode", colorCode);
		map.put("size", size);
		return goodsSizeDao.selectByGoodsIdAndColorcodeAndSize(map);
	}

	@Override
	public List<GoodsColor> selectGoodsColorByGoodsId(long goodsId) {
		return goodsSizeDao.selectGoodsColorInfo(goodsId);
	}

	@Override
	public GoodsSize selectbySizeId(long sizeCode) {
		return goodsSizeDao.selectByPrimaryId(sizeCode);
	}
	/**
	 * 根据订单维护商品数量
	 * @param orderId
	 */
	public void mainGoodsInventory(long orderId){
		List<OrderDetail> orderDetails = orderDetailService.selectByOrderId(orderId);
		if(!orderDetails.isEmpty()){
			Map<Long, Integer> goodsMap = new HashMap<>();
			try {
				int quantity = 0;
				Long goodsId = 0l;
				for (OrderDetail orderDetail : orderDetails) {
					goodsId = Long.parseLong(orderDetail.getGoodsId());
					quantity = orderDetail.getQuantity();
					try {
						updateInventoryToOccOrSalesNum(Long.parseLong(orderDetail.getSizeCode()),quantity,orderDetail.getOrderType());
					} catch (Exception e) {
						log.error(e.getMessage());
					}
					if(goodsMap.containsKey(goodsId)){
						quantity = goodsMap.get(goodsId) + quantity;
					}
					goodsMap.put(goodsId, quantity);
				}
				if(!goodsMap.isEmpty()){
					Goods goods;
					int inventoryNum = 0,buyNum=0;
					for (Long key : goodsMap.keySet()) {
						inventoryNum = 0;
						buyNum = 0;
						goods = goodsService.selectByGoodsId(key);
						if(goods != null){
							buyNum = goodsMap.get(key);
							if(goods.getInventoryNum()>= buyNum){
								inventoryNum = goods.getInventoryNum() - buyNum;
							}
							goods.setInventoryNum(inventoryNum);
							goods.setSaleNum(goods.getSaleNum()+buyNum);
							goods.setIorderNum(goods.getIorderNum()+1);
							try {
								goodsService.updateInventoryAndSaleAndOrderNum(goods);
							} catch (Exception e) {
								log.error(e.getMessage());
							}
							
						}
					}
				}
			} catch (Exception e) {
				log.error(e.getMessage());
			}finally{
				goodsMap = null;
			}
		}
	}
	
	@Transactional(propagation=Propagation.REQUIRES_NEW,rollbackFor = {RuntimeException.class,Exception.class})
	public boolean updateInventoryToOccOrSalesNum(long sizeCode, int saleNum,int goodsType) {
		boolean bret=false;
		if(sizeCode>0){
			try {
				GoodsSize goodsSize = goodsSizeDao.selectByPrimaryId(sizeCode);
				if(goodsSize == null){
					return bret;
				}
				
				int inventoryNum = 0;
				int occInventoryNum = 0;
				//非闪创商品需要修改库存
				if(goodsType!=3){
					if(goodsSize.getInventoryNum()>=saleNum){
						inventoryNum = goodsSize.getInventoryNum() - saleNum;
						occInventoryNum = goodsSize.getOccInventoryNum()+saleNum;
					}
				}else{
					inventoryNum = goodsSize.getInventoryNum();
				}
				int sumSaleNum = goodsSize.getSaleNum() + saleNum;
				
				bret = goodsSizeDao.updateInventoryToOccOrSalesNum(sizeCode,inventoryNum,occInventoryNum,sumSaleNum)>0?true:false;
			} catch (Exception e) {
				bret=false;
				log.error(e.getMessage());
				Throwables.propagate(e);
			}
		}
		return bret;
	}
	@Transactional(propagation=Propagation.REQUIRES_NEW,rollbackFor = {RuntimeException.class,Exception.class})
	public boolean updateOccInventoryToInventory(long sizeCode, int saleNum,int goodsType) {
		boolean bret=false;
		if(sizeCode>0){
			try {
				GoodsSize goodsSize = goodsSizeDao.selectByPrimaryId(sizeCode);
				if(goodsSize == null){
					return bret;
				}
				if(goodsType!=3){
					int occInventoryNum = 0;
					int inventoryNum = 0;
					if(goodsSize.getOccInventoryNum()>=saleNum){
						occInventoryNum = goodsSize.getOccInventoryNum()-saleNum;
						inventoryNum = goodsSize.getInventoryNum() + saleNum;
						bret = goodsSizeDao.updateInventoryToOccOrSalesNum(sizeCode,inventoryNum,occInventoryNum,null)>0?true:false;
					}
				}else{
					bret=true;
				}
			} catch (Exception e) {
				bret=false;
				log.error(e.getMessage());
				Throwables.propagate(e);
			}
		}
		return bret;
	}
}
